home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Programming / QuakeTools / src / libqtools / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-11  |  23.6 KB  |  513 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3.  
  4. //#define    DEBUG
  5. //#define    DEBUG_EXPAND
  6.  
  7. /*
  8.  * =============
  9.  * PrintBSPFileSizes
  10.  * 
  11.  * Dumps info about current file
  12.  * =============
  13.  */
  14. void PrintClusters(__memBase, int printType, bool toDisk)
  15. {
  16.   if (printType == 0)
  17.     printType = bspMem->availHeaders;
  18.   mprintf("----- Statistics --------\n");
  19.   if (bspMem->availHeaders & printType & LUMP_PLANES)
  20.     mprintf("%5i planes       %6i\n", bspMem->numplanes, (int)(bspMem->numplanes * sizeof(struct dplane_t)));
  21.  
  22.   if (bspMem->availHeaders & printType & LUMP_VERTEXES)
  23.     mprintf("%5i vertexes     %6i\n", bspMem->numvertexes, (int)(bspMem->numvertexes * sizeof(struct dvertex_t)));
  24.  
  25.   if (bspMem->availHeaders & printType & LUMP_NODES)
  26.     mprintf("%5i nodes        %6i\n", bspMem->numnodes, (int)(bspMem->numnodes * sizeof(struct dnode_t)));
  27.  
  28.   if (bspMem->availHeaders & printType & LUMP_TEXINFO)
  29.     mprintf("%5i texinfo      %6i\n", bspMem->numtexinfo, (int)(bspMem->numtexinfo * sizeof(struct texinfo)));
  30.  
  31.   if (bspMem->availHeaders & printType & LUMP_FACES)
  32.     mprintf("%5i faces        %6i\n", bspMem->numfaces, (int)(bspMem->numfaces * sizeof(struct dface_t)));
  33.  
  34.   if (bspMem->availHeaders & printType & LUMP_CLIPNODES)
  35.     mprintf("%5i clipnodes    %6i\n", bspMem->numclipnodes, (int)(bspMem->numclipnodes * sizeof(struct dclipnode_t)));
  36.  
  37.   if (bspMem->availHeaders & printType & LUMP_LEAFS)
  38.     mprintf("%5i leafs        %6i\n", bspMem->numleafs, (int)(bspMem->numleafs * sizeof(struct dleaf_t)));
  39.  
  40.   if (bspMem->availHeaders & printType & LUMP_MARKSURFACES)
  41.     mprintf("%5i marksurfaces %6i\n", bspMem->nummarksurfaces, (int)(bspMem->nummarksurfaces * sizeof(unsigned short int)));
  42.  
  43.   if (bspMem->availHeaders & printType & LUMP_SURFEDGES)
  44.     mprintf("%5i surfedges    %6i\n", bspMem->numsurfedges, (int)(bspMem->numsurfedges * sizeof(int)));
  45.  
  46.   if (bspMem->availHeaders & printType & LUMP_EDGES)
  47.     mprintf("%5i edges        %6i\n", bspMem->numedges, (int)(bspMem->numedges * sizeof(struct dedge_t)));
  48.  
  49.   if (bspMem->availHeaders & printType & LUMP_TEXTURES)
  50.     if (!bspMem->texdatasize)
  51.       mprintf("    0 textures          0\n");
  52.     else
  53.       mprintf("%5i textures     %6i\n", toDisk == TRUE ? LittleLong(((struct dmiptexlump_t *)bspMem->dtexdata)->nummiptex) : ((struct dmiptexlump_t *)bspMem->dtexdata)->nummiptex, bspMem->texdatasize);
  54.   if (bspMem->availHeaders & printType & LUMP_LIGHTING)
  55.     mprintf("      lightdata    %6i\n", bspMem->lightdatasize);
  56.   if (bspMem->availHeaders & printType & LUMP_VISIBILITY)
  57.     mprintf("      visdata      %6i\n", bspMem->visdatasize);
  58.   if (bspMem->availHeaders & printType & LUMP_ENTITIES)
  59.     mprintf("      entdata      %6i\n", bspMem->entdatasize);
  60.  
  61.   if (bspMem->availHeaders & printType & MAP_ENTITIES)
  62.     mprintf("%5i entities     %6i\n", bspMem->nummapentities, (int)(bspMem->nummapentities * sizeof(struct entity)));
  63.  
  64.   if (bspMem->availHeaders & printType & MAP_TEXSTRINGS)
  65.     mprintf("%5i texstrings   %6i\n", bspMem->nummaptexstrings, (int)(bspMem->nummaptexstrings * (sizeof(char *) + 16)));
  66.  
  67.   if (bspMem->availHeaders & printType & MAP_BRUSHFACES)
  68.     mprintf("%5i brushfaces  %6i\n", bspMem->numbrushfaces, (int)(bspMem->numbrushfaces * sizeof(struct mface)));
  69.  
  70.   if (bspMem->availHeaders & printType & MAP_BRUSHPLANES)
  71.     mprintf("%5i brushplanes  %6i\n", bspMem->numbrushplanes, (int)(bspMem->numbrushplanes * sizeof(struct plane)));
  72. }
  73.  
  74. /*
  75.  * =============
  76.  * AllocBSP
  77.  * =============
  78.  */
  79. void AllocClusters(__memBase, int allocType)
  80. {
  81.   if (bspMem) {
  82.     if (allocType & LUMP_ENTITIES) {
  83.       if (bspMem->dentdata)
  84.     tfree(bspMem->dentdata);
  85.       if (!(bspMem->dentdata = tmalloc((bspMem->max_entdatasize = CLUSTER_ENTSTRING) * sizeof(char))))
  86.     Error("failed to allocate bytes");
  87.       bspMem->entdatasize = 0;
  88.       oprintf("first cluster for entities allocated: %7d bytes (%lx)\n", CLUSTER_ENTSTRING * sizeof(char), bspMem->dentdata);
  89.     }
  90.     if (allocType & LUMP_PLANES) {
  91.       if (bspMem->dplanes)
  92.     tfree(bspMem->dplanes);
  93.       if (!(bspMem->dplanes = tmalloc((bspMem->max_numplanes = CLUSTER_PLANES) * sizeof(struct dplane_t))))
  94.     Error("failed to allocate bytes");
  95.       bspMem->numplanes = 0;
  96.       oprintf("first cluster for planes allocated: %7d bytes (%lx)\n", CLUSTER_PLANES * sizeof(struct dplane_t), bspMem->dplanes);
  97.     }
  98.     if (allocType & LUMP_TEXTURES) {
  99.       if (bspMem->dtexdata)
  100.     tfree(bspMem->dtexdata);
  101.       if (!(bspMem->dtexdata = tmalloc((bspMem->max_texdatasize = CLUSTER_TEXTURES) * sizeof(char))))
  102.     Error("failed to allocate bytes");
  103.       bspMem->texdatasize = 0;
  104.       oprintf("first cluster for textures allocated: %7d bytes (%lx)\n", CLUSTER_TEXTURES * sizeof(char), bspMem->dtexdata);
  105.     }
  106.     if (allocType & LUMP_VERTEXES) {
  107.       if (bspMem->dvertexes)
  108.     tfree(bspMem->dvertexes);
  109.       if (!(bspMem->dvertexes = tmalloc((bspMem->max_numvertexes = CLUSTER_VERTS) * sizeof(struct dvertex_t))))
  110.     Error("failed to allocate bytes");
  111.       bspMem->numvertexes = 0;
  112.       oprintf("first cluster for vertexes allocated: %7d bytes (%lx)\n", CLUSTER_VERTS * sizeof(struct dvertex_t), bspMem->dvertexes);
  113.     }
  114.     if (allocType & LUMP_VISIBILITY) {
  115.       if (bspMem->dvisdata)
  116.     tfree(bspMem->dvisdata);
  117.       if (!(bspMem->dvisdata = tmalloc((bspMem->max_visdatasize = CLUSTER_VISIBILITY) * sizeof(char))))
  118.     Error("failed to allocate bytes");
  119.       bspMem->visdatasize = 0;
  120.       oprintf("first cluster for visibility allocated: %7d bytes (%lx)\n", CLUSTER_VISIBILITY * sizeof(char), bspMem->dvisdata);
  121.     }
  122.     if (allocType & LUMP_NODES) {
  123.       if (bspMem->dnodes)
  124.     tfree(bspMem->dnodes);
  125.       if (!(bspMem->dnodes = tmalloc((bspMem->max_numnodes = CLUSTER_NODES) * sizeof(struct dnode_t))))
  126.     Error("failed to allocate bytes");
  127.       bspMem->numnodes = 0;
  128.       oprintf("first cluster for nodes allocated: %7d bytes (%lx)\n", CLUSTER_NODES * sizeof(struct dnode_t), bspMem->dnodes);
  129.     }
  130.     if (allocType & LUMP_TEXINFO) {
  131.       if (bspMem->texinfo)
  132.     tfree(bspMem->texinfo);
  133.       if (!(bspMem->texinfo = tmalloc((bspMem->max_numtexinfo = CLUSTER_TEXINFO) * sizeof(struct texinfo))))
  134.     Error("failed to allocate bytes");
  135.       bspMem->numtexinfo = 0;
  136.       oprintf("first cluster for texinfo allocated: %7d bytes (%lx)\n", CLUSTER_TEXINFO * sizeof(struct texinfo), bspMem->texinfo);
  137.     }
  138.     if (allocType & LUMP_FACES) {
  139.       if (bspMem->dfaces)
  140.     tfree(bspMem->dfaces);
  141.       if (!(bspMem->dfaces = tmalloc((bspMem->max_numfaces = CLUSTER_FACES) * sizeof(struct dface_t))))
  142.     Error("failed to allocate bytes");
  143.       bspMem->numfaces = 0;
  144.       oprintf("first cluster for faces allocated: %7d bytes (%lx)\n", CLUSTER_FACES * sizeof(struct dface_t), bspMem->dfaces);
  145.     }
  146.     if (allocType & LUMP_LIGHTING) {
  147.       if (bspMem->dlightdata)
  148.     tfree(bspMem->dlightdata);
  149.       if (!(bspMem->dlightdata = tmalloc((bspMem->max_lightdatasize = CLUSTER_LIGHTING) * sizeof(char))))
  150.     Error("failed to allocate bytes");
  151.       bspMem->lightdatasize = 0;
  152.       oprintf("first cluster for lighting allocated: %7d bytes (%lx)\n", CLUSTER_LIGHTING * sizeof(char), bspMem->dlightdata);
  153.     }
  154.     if (allocType & LUMP_CLIPNODES) {
  155.       if (bspMem->dclipnodes)
  156.     tfree(bspMem->dclipnodes);
  157.       if (!(bspMem->dclipnodes = tmalloc((bspMem->max_numclipnodes = CLUSTER_CLIPNODES) * sizeof(struct dclipnode_t))))
  158.     Error("failed to allocate bytes");
  159.       bspMem->numclipnodes = 0;
  160.       oprintf("first cluster for clipnodes allocated: %7d bytes (%lx)\n", CLUSTER_CLIPNODES * sizeof(struct dclipnode_t), bspMem->dclipnodes);
  161.     }
  162.     if (allocType & LUMP_LEAFS) {
  163.       if (bspMem->dleafs)
  164.     tfree(bspMem->dleafs);
  165.       if (!(bspMem->dleafs = tmalloc((bspMem->max_numleafs = CLUSTER_LEAFS) * sizeof(struct dleaf_t))))
  166.     Error("failed to allocate bytes");
  167.       bspMem->numleafs = 0;
  168.       oprintf("first cluster for leafs allocated: %7d bytes (%lx)\n", CLUSTER_LEAFS * sizeof(struct dleaf_t), bspMem->dleafs);
  169.     }
  170.     if (allocType & LUMP_MARKSURFACES) {
  171.       if (bspMem->dmarksurfaces)
  172.     tfree(bspMem->dmarksurfaces);
  173.       if (!(bspMem->dmarksurfaces = tmalloc((bspMem->max_nummarksurfaces = CLUSTER_MARKSURFACES) * sizeof(unsigned short int))))
  174.     Error("failed to allocate bytes");
  175.       bspMem->nummarksurfaces = 0;
  176.       oprintf("first cluster for marksurfaces allocated: %7d bytes (%lx)\n", CLUSTER_MARKSURFACES * sizeof(unsigned short int), bspMem->dmarksurfaces);
  177.     }
  178.     if (allocType & LUMP_EDGES) {
  179.       if (bspMem->dedges)
  180.     tfree(bspMem->dedges);
  181.       if (bspMem->edgefaces[0])
  182.     tfree(bspMem->edgefaces[0]);
  183.       if (bspMem->edgefaces[1])
  184.     tfree(bspMem->edgefaces[1]);
  185.     
  186.       if (!(bspMem->dedges = tmalloc((bspMem->max_numedges = CLUSTER_EDGES) * sizeof(struct dedge_t))))
  187.     Error("failed to allocate bytes");
  188.       if (!(bspMem->edgefaces[0] = tmalloc(CLUSTER_EDGES * sizeof(struct visfacet **))))
  189.       Error("failed to allocate bytes");
  190.       if (!(bspMem->edgefaces[1] = tmalloc(CLUSTER_EDGES * sizeof(struct visfacet **))))
  191.       Error("failed to allocate bytes");
  192.  
  193.       bspMem->numedges = 0;
  194.       oprintf("first cluster for edges allocated: %7d bytes (%lx)\n", CLUSTER_EDGES * sizeof(struct dedge_t), bspMem->dedges);
  195.       oprintf("first cluster for edgefaces[0] allocated: %7d bytes (%lx)\n", CLUSTER_EDGES * sizeof(struct visfacet **), bspMem->edgefaces[0]);
  196.       oprintf("first cluster for edgefaces[1] allocated: %7d bytes (%lx)\n", CLUSTER_EDGES * sizeof(struct visfacet **), bspMem->edgefaces[1]);
  197.     }
  198.     if (allocType & LUMP_SURFEDGES) {
  199.       if (bspMem->dsurfedges)
  200.     tfree(bspMem->dsurfedges);
  201.       if (!(bspMem->dsurfedges = tmalloc((bspMem->max_numsurfedges = CLUSTER_SURFEDGES) * sizeof(int))))
  202.     Error("failed to allocate bytes");
  203.       bspMem->numsurfedges = 0;
  204.       oprintf("first cluster for surfedges allocated: %7d bytes (%lx)\n", CLUSTER_SURFEDGES * sizeof(int), bspMem->dsurfedges);
  205.     }
  206.     if (allocType & LUMP_MODELS) {
  207.       if (bspMem->dmodels)
  208.     tfree(bspMem->dmodels);
  209.       if (!(bspMem->dmodels = tmalloc((bspMem->max_nummodels = CLUSTER_MODELS) * sizeof(struct dmodel_t))))
  210.     Error("failed to allocate bytes");
  211.       bspMem->nummodels = 0;
  212.       oprintf("first cluster for models allocated: %7d bytes (%lx)\n", CLUSTER_MODELS * sizeof(struct dmodel_t), bspMem->dmodels);
  213.     }
  214.     if (allocType & MAP_ENTITIES) {
  215.       if (bspMem->mapentities)
  216.     tfree(bspMem->mapentities);
  217.       if (!(bspMem->mapentities = tmalloc((bspMem->max_nummapentities = CLUSTER_ENTITIES) * sizeof(struct entity))))
  218.     Error("failed to allocate bytes");
  219.       bspMem->nummapentities = 0;
  220.       oprintf("first cluster for brushes allocated: %7d bytes (%lx)\n", CLUSTER_ENTITIES * sizeof(struct entity), bspMem->mapentities);
  221.     }
  222.     if (allocType & MAP_TEXSTRINGS) {
  223.       int i;
  224.       char *text;
  225.  
  226.       if (bspMem->maptexstrings)
  227.     tfree(bspMem->maptexstrings);
  228.       if (!(bspMem->maptexstrings = tmalloc((bspMem->max_nummaptexstrings = CLUSTER_TEXSTRINGS) * (sizeof(char *) + 16))))
  229.     Error("failed to allocate bytes");
  230.       bspMem->nummaptexstrings = 0;
  231.       for (i = 0, text = (char *)&bspMem->maptexstrings[bspMem->max_nummaptexstrings]; i < bspMem->max_nummaptexstrings; i++, text += 16)
  232.     bspMem->maptexstrings[i] = text;
  233.       oprintf("first cluster for texstrings allocated: %7d bytes (%lx)\n", CLUSTER_TEXSTRINGS * (sizeof(char *) + 16), bspMem->maptexstrings);
  234.     }
  235.     if (allocType & MAP_BRUSHFACES) {
  236.       if (bspMem->brushfaces)
  237.     tfree(bspMem->brushfaces);
  238.       if (!(bspMem->brushfaces = tmalloc((bspMem->max_numbrushfaces = CLUSTER_BRUSHFACES) * sizeof(struct mface))))
  239.     Error("failed to allocate bytes");
  240.       bspMem->numbrushfaces = 0;
  241.       oprintf("first cluster for brushfaces allocated: %7d bytes (%lx)\n", CLUSTER_BRUSHFACES * sizeof(struct mface), bspMem->brushfaces);
  242.     }
  243.     if (allocType & MAP_BRUSHPLANES) {
  244.       if (bspMem->brushplanes)
  245.     tfree(bspMem->brushplanes);
  246.       if (!(bspMem->brushplanes = tmalloc((bspMem->max_numbrushplanes = CLUSTER_BRUSHPLANES) * sizeof(struct plane))))
  247.     Error("failed to allocate bytes");
  248.       bspMem->numbrushplanes = 0;
  249.       oprintf("first cluster for brushes allocated: %7d bytes (%lx)\n", CLUSTER_BRUSHPLANES * sizeof(struct plane), bspMem->brushplanes);
  250.     }
  251.     bspMem->availHeaders |= allocType;
  252. #ifdef    CUSTOM_MAXIMA
  253.     if (!bspMem->maxpoints)
  254.       bspMem->maxpoints = DEFPOINTS;
  255.     if (!bspMem->maxedges)
  256.       bspMem->maxedges = DEFEDGES;
  257.     if (!bspMem->maxedges_in_region)
  258.       bspMem->maxedges_in_region = DEF_EDGES_IN_REGION;
  259.     if (!bspMem->maxportals)
  260.       bspMem->maxportals = DEF_PORTALS;
  261.     if (!bspMem->maxportals_in_leaf)
  262.       bspMem->maxportals_in_leaf = DEF_PORTALS_IN_LEAF;
  263. #endif
  264.   }
  265. }
  266.  
  267. /*
  268.  * =============
  269.  * ExpandBSP
  270.  * =============
  271.  * for all use trealloc instead ?
  272.  */
  273. void ExpandClusters(__memBase, int allocType)
  274. {
  275.   if (bspMem) {
  276.     if (allocType & LUMP_ENTITIES) {
  277.       if (!(bspMem->dentdata = trealloc(bspMem->dentdata, (bspMem->max_entdatasize += CLUSTER_ENTSTRING) * sizeof(char))))
  278.     Error("failed to allocate bytes");
  279.       oprintf("additional cluster for entities allocated: %7d bytes (%lx)\n", bspMem->max_entdatasize * sizeof(char), bspMem->dentdata);
  280.     }
  281.     if (allocType & LUMP_PLANES) {
  282.       if (!(bspMem->dplanes = trealloc(bspMem->dplanes, (bspMem->max_numplanes += CLUSTER_PLANES) * sizeof(struct dplane_t))))
  283.     Error("failed to allocate bytes");
  284.     oprintf("additional cluster for planes allocated: %7d bytes (%lx)\n", bspMem->max_numplanes * sizeof(struct dplane_t), bspMem->dplanes);
  285.     }
  286.     if (allocType & LUMP_TEXTURES) {
  287.       if (!(bspMem->dtexdata = trealloc(bspMem->dtexdata, (bspMem->max_texdatasize += CLUSTER_TEXTURES) * sizeof(char))))
  288.     Error("failed to allocate bytes");
  289.     oprintf("additional cluster for textures allocated: %7d bytes (%lx)\n", bspMem->max_texdatasize * sizeof(char), bspMem->dtexdata);
  290.     }
  291.     if (allocType & LUMP_VERTEXES) {
  292.       if (!(bspMem->dvertexes = trealloc(bspMem->dvertexes, (bspMem->max_numvertexes += CLUSTER_VERTS) * sizeof(struct dvertex_t))))
  293.     Error("failed to allocate bytes");
  294.     oprintf("additional cluster for vertexes allocated: %7d bytes (%lx)\n", bspMem->max_numvertexes * sizeof(struct dvertex_t), bspMem->dvertexes);
  295.     }
  296.     if (allocType & LUMP_VISIBILITY) {
  297.       if (!(bspMem->dvisdata = trealloc(bspMem->dvisdata, (bspMem->max_visdatasize += CLUSTER_VISIBILITY) * sizeof(char))))
  298.     Error("failed to allocate bytes");
  299.     oprintf("additional cluster for visibility allocated: %7d bytes (%lx)\n", bspMem->max_visdatasize * sizeof(char), bspMem->dvisdata);
  300.     }
  301.     if (allocType & LUMP_NODES) {
  302.       if ((bspMem->dnodes = trealloc(bspMem->dnodes, (bspMem->max_numnodes += CLUSTER_NODES) * sizeof(struct dnode_t))))
  303.     Error("failed to allocate bytes");
  304.     oprintf("additional cluster for nodes allocated: %7d bytes (%lx)\n", bspMem->max_numnodes * sizeof(struct dnode_t), bspMem->dnodes);
  305.     }
  306.     if (allocType & LUMP_TEXINFO) {
  307.       if (!(bspMem->texinfo = trealloc(bspMem->texinfo, (bspMem->max_numtexinfo += CLUSTER_TEXINFO) * sizeof(struct texinfo))))
  308.     Error("failed to allocate bytes");
  309.     oprintf("additional cluster for texinfo allocated: %7d bytes (%lx)\n", bspMem->max_numtexinfo * sizeof(struct texinfo), bspMem->texinfo);
  310.     }
  311.     if (allocType & LUMP_FACES) {
  312.       if (!(bspMem->dfaces = trealloc(bspMem->dfaces, (bspMem->max_numfaces += CLUSTER_FACES) * sizeof(struct dface_t))))
  313.     Error("failed to allocate bytes");
  314.     oprintf("additional cluster for faces allocated: %7d bytes (%lx)\n", bspMem->max_numfaces * sizeof(struct dface_t), bspMem->dfaces);
  315.     }
  316.     if (allocType & LUMP_LIGHTING) {
  317.       if (!(bspMem->dlightdata = trealloc(bspMem->dlightdata, (bspMem->max_lightdatasize += CLUSTER_LIGHTING) * sizeof(char))))
  318.     Error("failed to allocate bytes");
  319.     oprintf("additional cluster for lighting allocated: %7d bytes (%lx)\n", bspMem->max_lightdatasize * sizeof(char), bspMem->dlightdata);
  320.     }
  321.     if (allocType & LUMP_CLIPNODES) {
  322.       if (!(bspMem->dclipnodes = trealloc(bspMem->dclipnodes, (bspMem->max_numclipnodes += CLUSTER_CLIPNODES) * sizeof(struct dclipnode_t))))
  323.     Error("failed to allocate bytes");
  324.     oprintf("additional cluster for clipnodes allocated: %7d bytes (%lx)\n", bspMem->max_numclipnodes * sizeof(struct dclipnode_t), bspMem->dclipnodes);
  325.     }
  326.     if (allocType & LUMP_LEAFS) {
  327.       if (!(bspMem->dleafs = trealloc(bspMem->dleafs, (bspMem->max_numleafs += CLUSTER_LEAFS) * sizeof(struct dleaf_t))))
  328.     Error("failed to allocate bytes");
  329.     oprintf("additional cluster for leafs allocated: %7d bytes (%lx)\n", bspMem->max_numleafs * sizeof(struct dleaf_t), bspMem->dleafs);
  330.     }
  331.     if (allocType & LUMP_MARKSURFACES) {
  332.       if (!(bspMem->dmarksurfaces = trealloc(bspMem->dmarksurfaces, (bspMem->max_nummarksurfaces += CLUSTER_MARKSURFACES) * sizeof(unsigned short int))))
  333.     Error("failed to allocate bytes");
  334.     oprintf("additional cluster for marksurfaces allocated: %7d bytes (%lx)\n", bspMem->max_nummarksurfaces * sizeof(unsigned short int), bspMem->dmarksurfaces);
  335.     }
  336.     if (allocType & LUMP_EDGES) {
  337.       if (!(bspMem->dedges = trealloc(bspMem->dedges, (bspMem->max_numedges += CLUSTER_EDGES) * sizeof(struct dedge_t))))
  338.     Error("failed to allocate bytes");
  339.       if (!(bspMem->edgefaces[0] = trealloc(bspMem->edgefaces[0], bspMem->max_numedges * sizeof(struct visfacet **))))
  340.     Error("failed to allocate bytes");
  341.       if (!(bspMem->edgefaces[1] = trealloc(bspMem->edgefaces[1], bspMem->max_numedges * sizeof(struct visfacet **))))
  342.     Error("failed to allocate bytes");
  343.       oprintf("additional cluster for edges allocated: %7d bytes (%lx)\n", bspMem->max_numedges * sizeof(struct dedge_t), bspMem->dedges);
  344.       oprintf("additional cluster for edgefaces[0] allocated: %7d bytes (%lx)\n", bspMem->max_numedges * sizeof(struct visfacet **), bspMem->edgefaces[0]);
  345.       oprintf("additional cluster for edgefaces[1] allocated: %7d bytes (%lx)\n", bspMem->max_numedges * sizeof(struct visfacet **), bspMem->edgefaces[1]);
  346.     }
  347.     if (allocType & LUMP_SURFEDGES) {
  348.       if (!(bspMem->dsurfedges = trealloc(bspMem->dsurfedges, (bspMem->max_numsurfedges += CLUSTER_SURFEDGES) * sizeof(int))))
  349.     Error("failed to allocate bytes");
  350.     oprintf("additional cluster for surfedges allocated: %7d bytes (%lx)\n", bspMem->max_numsurfedges * sizeof(int), bspMem->dsurfedges);
  351.     }
  352.     if (allocType & LUMP_MODELS) {
  353.       if (!(bspMem->dmodels = trealloc(bspMem->dmodels, (bspMem->max_nummodels += CLUSTER_MODELS) * sizeof(struct dmodel_t))))
  354.     Error("failed to allocate bytes");
  355.     oprintf("additional cluster for models allocated: %7d bytes (%lx)\n", bspMem->max_nummodels * sizeof(struct dmodel_t), bspMem->dmodels);
  356.     }
  357.     if (allocType & MAP_ENTITIES) {
  358.       if (!(bspMem->mapentities = trealloc(bspMem->mapentities, (bspMem->max_nummapentities += CLUSTER_ENTITIES) * sizeof(struct entity))))
  359.     Error("failed to allocate bytes");
  360.     oprintf("additional cluster for mapentities allocated: %7d bytes (%lx)\n", bspMem->max_nummapentities * sizeof(struct entity), bspMem->mapentities);
  361.     }
  362.     if (allocType & MAP_TEXSTRINGS) {
  363.       int i;
  364.       char *text;
  365.       char **maptexstrings = bspMem->maptexstrings;
  366.       if (!(bspMem->maptexstrings = tmalloc((bspMem->max_nummaptexstrings += CLUSTER_TEXSTRINGS) * (sizeof(char *) + 16))))
  367.     Error("failed to allocate bytes");
  368.     
  369.       for (i = 0, text = (char *)&bspMem->maptexstrings[bspMem->max_nummaptexstrings]; i < bspMem->max_nummaptexstrings; i++, text += 16)
  370.     bspMem->maptexstrings[i] = text;
  371.       memcpy(bspMem->maptexstrings[0], maptexstrings[0], bspMem->nummaptexstrings * 16);
  372.       tfree(maptexstrings);
  373.     oprintf("additional cluster for mapstrings allocated: %7d bytes (%lx)\n", bspMem->max_nummaptexstrings * (sizeof(char *) + 16), bspMem->maptexstrings);
  374.     }
  375.     if (allocType & MAP_BRUSHFACES) {
  376.       if (!(bspMem->brushfaces = trealloc(bspMem->brushfaces, (bspMem->max_numbrushplanes += CLUSTER_BRUSHFACES) * sizeof(struct mface))))
  377.     Error("failed to allocate bytes");
  378.     oprintf("additional cluster for brushplanes allocated: %7d bytes (%lx)\n", bspMem->max_numbrushplanes * sizeof(struct mface), bspMem->brushfaces);
  379.     }
  380.     if (allocType & MAP_BRUSHPLANES) {
  381.       if (!(bspMem->brushplanes = trealloc(bspMem->brushplanes, (bspMem->max_numbrushplanes += CLUSTER_BRUSHPLANES) * sizeof(struct plane))))
  382.     Error("failed to allocate bytes");
  383.     oprintf("additional cluster for brushplanes allocated: %7d bytes (%lx)\n", bspMem->max_numbrushplanes * sizeof(struct plane), bspMem->brushplanes);
  384.     }
  385.     bspMem->availHeaders |= allocType;
  386. #ifdef    CUSTOM_MAXIMA
  387.     if (!bspMem->maxpoints)
  388.       bspMem->maxpoints = DEFPOINTS;
  389.     if (!bspMem->maxedges)
  390.       bspMem->maxedges = DEFEDGES;
  391.     if (!bspMem->maxedges_in_region)
  392.       bspMem->maxedges_in_region = DEF_EDGES_IN_REGION;
  393.     if (!bspMem->maxportals)
  394.       bspMem->maxportals = DEF_PORTALS;
  395.     if (!bspMem->maxportals_in_leaf)
  396.       bspMem->maxportals_in_leaf = DEF_PORTALS_IN_LEAF;
  397. #endif
  398.   }
  399. }
  400.  
  401. /*
  402.  * =============
  403.  * FreeBSP
  404.  * =============
  405.  */
  406. void FreeClusters(__memBase, int freeType)
  407. {
  408.   if (bspMem) {
  409.     if (!freeType)
  410.       freeType = bspMem->availHeaders;
  411.  
  412.     oprintf("free entities\n");
  413.     if ((bspMem->availHeaders & freeType & LUMP_ENTITIES))
  414.       tfree(bspMem->dentdata);
  415.     oprintf("free planes\n");
  416.     if ((bspMem->availHeaders & freeType & LUMP_PLANES))
  417.       tfree(bspMem->dplanes);
  418.     oprintf("free textures\n");
  419.     if ((bspMem->availHeaders & freeType & LUMP_TEXTURES))
  420.       tfree(bspMem->dtexdata);
  421.     oprintf("free vertexes\n");
  422.     if ((bspMem->availHeaders & freeType & LUMP_VERTEXES))
  423.       tfree(bspMem->dvertexes);
  424.     oprintf("free visibility\n");
  425.     if ((bspMem->availHeaders & freeType & LUMP_VISIBILITY))
  426.       tfree(bspMem->dvisdata);
  427.     oprintf("free nodes\n");
  428.     if ((bspMem->availHeaders & freeType & LUMP_NODES))
  429.       tfree(bspMem->dnodes);
  430.     oprintf("free texinfo\n");
  431.     if ((bspMem->availHeaders & freeType & LUMP_TEXINFO))
  432.       tfree(bspMem->texinfo);
  433.     oprintf("free faces\n");
  434.     if ((bspMem->availHeaders & freeType & LUMP_FACES))
  435.       tfree(bspMem->dfaces);
  436.     oprintf("free lighting\n");
  437.     if ((bspMem->availHeaders & freeType & LUMP_LIGHTING))
  438.       tfree(bspMem->dlightdata);
  439.     oprintf("free clipnodes\n");
  440.     if ((bspMem->availHeaders & freeType & LUMP_CLIPNODES))
  441.       tfree(bspMem->dclipnodes);
  442.     oprintf("free leafs\n");
  443.     if ((bspMem->availHeaders & freeType & LUMP_LEAFS))
  444.       tfree(bspMem->dleafs);
  445.     oprintf("free marksurfaces\n");
  446.     if ((bspMem->availHeaders & freeType & LUMP_MARKSURFACES))
  447.       tfree(bspMem->dmarksurfaces);
  448.     oprintf("free edges\n");
  449.     if ((bspMem->availHeaders & freeType & LUMP_EDGES)) {
  450.       tfree(bspMem->dedges);
  451.       if (bspMem->edgefaces[0])
  452.     tfree(bspMem->edgefaces[0]);
  453.       if (bspMem->edgefaces[1])
  454.     tfree(bspMem->edgefaces[1]);
  455.     }
  456.     oprintf("free surfedges\n");
  457.     if ((bspMem->availHeaders & freeType & LUMP_SURFEDGES))
  458.       tfree(bspMem->dsurfedges);
  459.     oprintf("free models\n");
  460.     if ((bspMem->availHeaders & freeType & LUMP_MODELS))
  461.       tfree(bspMem->dmodels);
  462.  
  463.     oprintf("free entities\n");
  464.     if ((bspMem->availHeaders & freeType & MAP_ENTITIES)) {
  465.       struct entity *ent;
  466.       int i;
  467.  
  468.       for (i = 0, ent = bspMem->mapentities; i < bspMem->nummapentities; i++, ent++) {
  469.     struct epair *ep = ent->epairs;
  470.     struct mbrush *mbr = ent->brushes;
  471.  
  472.     while (ep) {
  473.       struct epair *en = ep->next;
  474.  
  475.       tfree(ep->key);
  476.       tfree(ep->value);
  477.       tfree(ep);
  478.       ep = en;
  479.     }
  480.     while (mbr) {
  481.       struct mbrush *mbn = mbr->next;
  482.       struct mface *mfc = mbr->faces;
  483.  
  484.       while (mfc) {
  485.         struct mface *mfn = mfc->next;
  486.  
  487.         tfree(mfc);
  488.         mfc = mfn;
  489.       }
  490.       tfree(mbr);
  491.       mbr = mbn;
  492.     }
  493.       }
  494.       tfree(bspMem->mapentities);
  495.     }
  496.     oprintf("free texstrings\n");
  497.     if ((bspMem->availHeaders & freeType & MAP_TEXSTRINGS))
  498.       tfree(bspMem->maptexstrings);
  499.     oprintf("free brushfaces\n");
  500.     if ((bspMem->availHeaders & freeType & MAP_BRUSHFACES))
  501.       tfree(bspMem->brushfaces);
  502.     oprintf("free brushmaps\n");
  503.     if ((bspMem->availHeaders & freeType & MAP_BRUSHPLANES))
  504.       tfree(bspMem->brushplanes);
  505.  
  506.     /*
  507.      * probably this is not ours;
  508.      * tfree(bspMem);
  509.      */
  510.   }
  511.   bspMem->availHeaders &= ~freeType;
  512. }
  513.